<?
//这里就是打包数据，然后post过去发送模板信息的页面

function send_wx_sms($url, $info)
{
    $res = triggerRequest($url, $info);
    return $res;
}


function request_post($url = '', $post_data = array())
{
    if (empty($url) || empty($post_data)) {
        return false;
    }

    $o = "";
    foreach ($post_data as $k => $v) {
        $o .= "$k=" . urlencode($v) . "&";
    }
    $post_data = substr($o, 0, -1);

    $postUrl = $url;

    $curlPost = $post_data;
    $ch       = curl_init();//初始化curl
    curl_setopt($ch, CURLOPT_URL, $postUrl);//抓取指定网页
    curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
    curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
    $data = curl_exec($ch);//运行curl
    curl_close($ch);

    return $data;
}


function triggerRequest($url, $post_data = array(), $cookie = array()){
    $method = "GET";  //通过POST或者GET传递一些参数给要触发的脚本
    $url_array = parse_url($url); //获取URL信息
    $port = isset($url_array['port'])? $url_array['port'] : 80;
    $fp = fsockopen($url_array['host'], $port, $errno, $errstr, 30);
    if (!$fp) {
        return FALSE;
    }
    $getPath = $url_array['path'] ."?". $url_array['query'];
    if(!empty($post_data)){
        $method = "POST";
    }
    $header = $method . " " . $getPath;
    $header .= " HTTP/1.1\r\n";
    $header .= "Host: ". $url_array['host'] . "\r\n "; //HTTP 1.1 Host域不能省略

    $header .= "Connection:Close\r\n";
    if(!empty($cookie)){
        $_cookie = strval(NULL);
        foreach($cookie as $k => $v){
            $_cookie .= $k."=".$v."; ";
        }
        $cookie_str =  "Cookie: " . base64_encode($_cookie) ." \r\n"; //传递Cookie
        $header .= $cookie_str;
    }
    if(!empty($post_data)){
        $_post = strval(NULL);
        foreach($post_data as $k => $v){
            $_post .= $k."=".$v."&";
        }
        $post_str  = "Content-Type: application/x-www-form-urlencoded\r\n";
        $post_str .= "Content-Length: ". strlen($_post) ." \r\n"; //POST数据的长度
        $post_str .= $_post."\r\n\r\n "; //传递POST数据
        $header .= $post_str;
    }
    fwrite($fp, $header);
    //echo fread($fp, 1024); //服务器返回
    fclose($fp);
    return true;
}

?>